/**
 * Posts data.
 *
 * @param data
 */
function postData(data, title, action, flag) {
    if (flag == undefined) {
        flag == false;
    }
    var formId = "form-" + new Date().getTime();
    var titleId = formId + "-title";
    var dataId = formId + "-data";
    var flagId = formId + "-flag";
    var html = '<form id="' + formId + '" style="position:absolute;z-index:-1122;" method="post" action="'+ action +'">';
    html += '<input id="' + titleId + '" name="title" type="hidden">';
    html += '<input id="' + dataId + '" name="data" type="hidden">';
    html += '<input id="' + flagId + '" name="flag" type="hidden">';
    html += '</form>';

    if ($("#" + formId).length > 0) {
        $("#" + formId).remove();
    }

    $(html).appendTo("body");
    $("#" + titleId).val(title);
    $("#" + dataId).val(data);
    $("#" + flagId).val(flag);
    $("#" + formId).submit();
    $("#" + formId).remove();

}


/**
 * Generates excel by the data of grid.
 *
 * @param grid
 *            CSS selector of the <code>table</code> element, such as
 *            #selector, .selector, etc.
 */
function generateExcel(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }

    var matrix = [];
    matrix.push(colNames);

    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];

        // Extract the value fields.
        for (var k in data[j]) {
            row.push(data[j][k]);
        }

        matrix.push(row);
    }
    postData(JSON.stringify(matrix), title);

}


/**
 * Adds a generate excel button to the grid's paging bar. <strong>This method
 * must be invoked after the jQGrid's navGrid method, or the navGrid method
 * would not take effect.</strong>
 *
 * @param grid
 *            destination grid selector
 * @param pager
 *            the paging bar selector of the grid
 */
function addGenerateButton(grid, pager) {
    $("#grid").jqGrid('navGrid', "#pager", {
        add: false,
        edit: false,
        del: false,
        search: false,
        refresh: false
    });
    $(grid).jqGrid('navButtonAdd', pager, {
        caption: "",
        buttonicon: "ui-icon-document-excel",
        onClickButton: function () {
            generateExcel(grid);
        },
        position: "last",
        title: "导出数据",
        cursor: "pointer"
    });
}

function generateSchtolExcel(grid, title) {
    var colNames = $(grid).jqGrid("getGridParam", "colNames");
    var re = /[<>]+/;
    var matrix = [];
    var header = [];
    for (var i = 2; i < colNames.length; i += 1) {
        header[i - 2] = colNames[i];
    }
    matrix.push(header);
    var data = $(grid).jqGrid("getRowData");
    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        // Extract the value fields.
        for (var k in data[j]) {
            if (k != "modifyItem") {
                row.push(data[j][k]);
            }
        }
        matrix.push(row);
    }
    postData(JSON.stringify(matrix), title);
}
//alarm list export
function generateExcelForAalarm(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = []
    var matrix = [];
    for (var a = 7; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            if (k != 'alarm_seq_id' && k != 'alarm_type' && k != 'alarm_stat'){
                row.push(data[j][k]);
            }
        }
        for (var b = 3; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);

}

//alarm list export (oos ooc)
function generateExcelForAalarm1(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = []
    var matrix = [];
    for (var a = 2; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            row.push(data[j][k]);
        }
        for (var b = 2; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);

}

//alarm list export (rule)
function generateExcelForAalarm2(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = []
    var matrix = [];
    for (var a = 1; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            row.push(data[j][k]);
        }
        for (var b = 1; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);

}

//glass list export
function generateExcelForGlass(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = [];
    var matrix = [];
    for (var a = 1; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            row.push(data[j][k]);
        }
        for (var b = 0; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);

}
//panel list export
function generateExcelForPanel(grid, title) {

    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = []
    var matrix = [];
    for (var a = 0; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            row.push(data[j][k]);
        }
        for (var b = 0; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);

}

//chart图 defect code export
function generateExcelForDefectCode (grid, title){
    var colNames = $(grid).jqGrid("getGridParam", "colNames");

    // Filter the HTML entry columns.
    var re = /[<>]+/;

    for (var i = 0; i < colNames.length; i += 1) {
        if (re.test(colNames[i]) || colNames[i] === "") {
            colNames.splice(i, 1);
        }
    }
    var colNamesAct = []
    var matrix = [];
    for (var a = 0; a < colNames.length; a += 1) {
        colNamesAct.push(colNames[a]);
    }
    matrix.push(colNamesAct);
    var data = $(grid).jqGrid("getRowData");

    for (var j = 0; j < data.length; j += 1) {
        var row = [];
        var rowAct = [];
        for (var k in data[j]) {
            row.push(data[j][k]);
        }
        for (var b = 0; b < row.length; b += 1) {
            rowAct.push(row[b]);
        }
        matrix.push(rowAct);
    }
    postData(JSON.stringify(matrix), title);
}

//基础信息维护
function generateBasicDataExcel(dataCate,title) {
    var tbl_cnt, iary, inTrxObj, outTrxObj;
    var colNames = [];

    var iary = [{
        data_cate : "CODE",
        data_id   : dataCate
    }];
    var inTrxObj = {
        trx_id     : 'XPBASICDATA',
        action_flg : "Q"        ,
        iary       : iary
    };
    outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if( outTrxObj.rtn_code == _NORMAL ) {
        for (var i = 0; i < outTrxObj.tbl_cnt; i++) {
            colNames.push(outTrxObj.oary[i].dataDesc.trim());
            // oary.dataExt
        }
    }

    var matrix = [];
    matrix.push(colNames);
    postData(JSON.stringify(matrix),title, "exportExcel.do", false);

}